This invention relates to the run-time efficiency of computer programs and, more specifically, to a method for optimizing parameter passing.
An important consideration in many computer programs and programming languages is performance. Speed and correctness are examples of performance metrics, and are critical to the viability of computation intensive computer programs.
The quest for greater run-time speeds has often focussed on ways that information or data and data structures can be accessed and manipulated. Often, computer programs include routines that carry out well-defined operations on data specified by parameters. As used herein, the term xe2x80x9croutinexe2x80x9d refers to functions, procedures, methods and/or subroutines. To transfer control to a routine, a call is made to the routine. For the purposes of explanation, the portion of code that makes a call to a routine will be referred to as the xe2x80x9ccalling routinexe2x80x9d, while the portion of the code thus called is the xe2x80x9ccalled routinexe2x80x9d. Typically, parameters are provided from the calling routine to the called routine as input for each call.
The act of passing parameter values to a called routine is known as xe2x80x9cparameter passingxe2x80x9d. Two common methods of parameter passing are xe2x80x9ccall by valuexe2x80x9d, and xe2x80x9ccall by referencexe2x80x9d. The following definitions aid in explaining parameter passing. A xe2x80x9cformal parameterxe2x80x9d is a parameter value stored in a storage location that is used by the called routine. An xe2x80x9cactual parameterxe2x80x9d is a parameter value stored in a storage location used by the calling routine. From the perspective of the calling routine, the xe2x80x9cactual parameterxe2x80x9d is passed to and/or received from the called routine. From the perspective of the called routine, the xe2x80x9cformal parameterxe2x80x9d is received from and/or passed to the calling routine.
In the case of xe2x80x9ccall by valuexe2x80x9d, the actual parameter and the corresponding formal parameter are stored in separate storage locations. Specifically, the value of the actual parameter in the calling routine is copied to a separate storage allocated for holding the formal parameter in the called routine. Therefore, any changes made to the formal parameters of the routine modify only the copy of the actual parameter. Only upon successful completion of execution of the called routine, the modified value of the formal parameter is copied back to the actual parameter. Thus, when the called routine fails with an unanticipated error (xe2x80x9cunhandled exceptionxe2x80x9d), the modified value of the formal parameter is not copied back to the actual parameter. This ensures that the actual parameter remains uncorrupted by any exception occurring during execution of the called routine. Note that for anticipated errors (handled exceptions), it is assumed that the proper recovery mechanism is employed.
However, the disadvantage of xe2x80x9ccall by valuexe2x80x9d parameter passing is that it is inefficient. As explained above, in executing a call, two copy operations are performed for each parameter for xe2x80x9ccall by valuexe2x80x9d. The first copy is performed when the value of the actual parameter is copied to a separate storage allocated for holding the formal parameter of the routine before execution of the routine (copy-in). The second copy is performed when the modified value of the formal parameter of the called routine is copied back to the actual parameter upon successful completion of execution of the called routine (copy-out). Thus, the copy-in and copy-out transactions incurred in xe2x80x9ccall by valuexe2x80x9d parameter passing are inefficient. This is of particular concern in database systems where applications manipulate large volumes of data and where a parameter can comprise a very large data structure, such as a large record, or large array.
In the case of xe2x80x9ccall by referencexe2x80x9d, the same storage location is used for both the actual and formal parameters. Specifically, the address of the actual parameter of the calling routine is passed to the called routine. Any changes made by the called routine to the formal parameter directly modify the actual parameter. There is no copy-in and copy-out of the parameters. Because no copies of the values of the parameters are required in xe2x80x9ccall by referencexe2x80x9d parameter passing, it is more efficient than xe2x80x9ccall by valuexe2x80x9d parameter passing. However, there are at least two disadvantages to xe2x80x9ccall by referencexe2x80x9d parameter passing.
One obvious disadvantage to xe2x80x9ccall by referencexe2x80x9d parameter passing is that the routine directly modifies the actual parameter as explained above. Thus, when the called routine fails due to an unhandled exception, whatever undesirable modifications made by the called routine are made directly to the actual parameters.
Another disadvantage to xe2x80x9ccall by referencexe2x80x9d parameter passing is in the context of aliases. Two parameters in a program are said to be aliased if they both point to the same memory location. This can cause unintended results. For example, consider a procedure that has two parameters. Suppose the same parameter is passed twice in the same call to the procedure using xe2x80x9ccall by referencexe2x80x9d as illustrated in the following pseudocode, interspersed with explanatory comments denoted by the prefix, xe2x80x9c - - - xe2x80x9d:
main ( )
n number;
begin
n:=10;
proc (n, n); - - - call to the routine proc end;
Assume that the routine proc is as follows:
proc (p1 number, p2 number) is
begin
p1:=5;
print (p2);
end;
The print statement in the above routine will produce the value 5 if p1 and p2 are passed using xe2x80x9ccall by referencexe2x80x9d. Both p1 and p2 point to the same memory location, i.e. the memory location of the actual parameter, n. The first assignment,
p1:=5;
would modify the value of p2 as well. Clearly, this produces an unintended result.
Note that if the p1 and p2 were passed to routine proc using xe2x80x9ccall by valuexe2x80x9d parameter passing, the print statement would produce the value 10. Even though the same variable n was passed twice to the routine proc as actual parameters, two separate copies of the actual parameter, one bound to p1 and the other bound to p2, were made before execution of routine proc. Thus, the following assignment would only modify the copy of the actual parameter that is bound to p1.
p1:=5;
However, the copy of the actual parameter that is bound to p2 remained intact. Thus, the following print statement would produce the value 10.
print (p2);
To summarize, xe2x80x9ccall by valuexe2x80x9d parameter passing has the advantage of clean semantics in the context of exception handling and aliases. However, this advantage comes at the expense of performance. Whereas xe2x80x9ccall by referencexe2x80x9d parameter passing has the advantage of good performance but may produce unintended results in the context of aliases. xe2x80x9cCall by referencexe2x80x9d parameter passing may also cause corruption of actual parameters due to unhandled exceptions.
Alias detection and prevention is a well-researched problem. A number of techniques have been suggested in the literature. An appropriate technique can be used in conjunction with this invention to determine the presence of aliasing among parameters as well as among global variables and subprogram parameters.
Based on the foregoing, there is a need for a method or mechanism to take advantage of better performance provided by xe2x80x9ccall by referencexe2x80x9d parameter passing while obviating unintended results in the context of aliases and corruption of actual parameters due to unhandled exceptions.
The foregoing needs are addressed by the present invention, which comprises, in one aspect, a method and system for optimizing the passing of a parameter to a routine. It is determined if any parameters are aliased in a call to a called routine. In the absence of aliases, calling routines pass parameters to called routines using xe2x80x9ccall by referencexe2x80x9d. Recovery mechanisms are provided, when appropriate, when unhandled exceptions occur during execution of the called routines.
According to one aspect of the invention, a recovery mechanism includes encapsulating the called routine and call statement block with a xe2x80x9ccatch-allxe2x80x9d exception handler. Another aspect of the invention includes responding to an otherwise unhandled exception by restoring the actual parameter using a stored copy of the actual parameter. In one embodiment, the restoration is performed by making a handle (pointer) point to the copy of the actual parameter that was made before execution of the called routine.
Still other objects and advantages of the present invention will become readily apparent from the following detailed description, simply by way of illustration of the best mode contemplated of carrying out the invention. As will be realized, the invention is capable of other and different embodiments and its several details are capable of modifications in various obvious respects, all without departing from the invention. Accordingly, the drawing and description are to be regarded as illustrative in nature, and not as restrictive.